#!/bin/bash
set -ex
OPENSTACK_VERSION="stable/pike"
IMAGE_TAG="${OPENSTACK_VERSION#*/}"
FROM="starlingx4arm/ubuntu:slim-aarch64"

sudo docker run -d \
  --name docker-in-docker \
  --privileged=true \
  --net=host \
  -v $(pwd)/loci/:/loci \
  -v /var/lib/docker \
  -v ${HOME}/.docker/config.json:/root/.docker/config.json:ro\
  docker.io/docker:dind \
  dockerd \
    --pidfile=/var/run/docker.pid \
    --host=unix:///var/run/docker.sock \
    --storage-driver=overlay2
sudo docker exec docker-in-docker sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
sudo docker exec docker-in-docker apk update
sudo docker exec docker-in-docker apk add git

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --network host \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT=requirements \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --tag docker.io/starlingx4arm/requirements:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/requirements:${IMAGE_TAG}

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=keystone \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/keystone.git \
    --build-arg PROFILES="fluent apache ldap" \
    --build-arg PIP_PACKAGES="pycrypto python-openstackclient" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/keystone:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/keystone:${IMAGE_TAG}

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=heat \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/heat.git \
    --build-arg PROFILES="fluent apache" \
    --build-arg PIP_PACKAGES="pycrypto" \
    --build-arg DIST_PACKAGES="curl" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/heat:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/heat:${IMAGE_TAG}

# sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
#     /loci \
#     --build-arg PROJECT=barbican \
#     --build-arg FROM=${FROM} \
#     --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
#     --build-arg PROFILES="fluent" \
#     --build-arg PIP_PACKAGES="pycrypto" \
#     --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
#     --tag docker.io/starlingx4arm/barbican:${IMAGE_TAG}
# sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/barbican:${IMAGE_TAG}

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=glance \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/glance.git \
    --build-arg PROFILES="fluent glance ceph" \
    --build-arg PIP_PACKAGES="pycrypto python-swiftclient" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/glance:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/glance:${IMAGE_TAG}

# sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
#     /loci \
#     --build-arg PROJECT=cinder \
#     --build-arg FROM=${FROM} \
#     --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
#     --build-arg PROFILES="fluent cinder lvm ceph qemu" \
#     --build-arg PIP_PACKAGES="pycrypto python-swiftclient" \
#     --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
#     --tag docker.io/starlingx4arm/cinder:${IMAGE_TAG}
# sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/cinder:${IMAGE_TAG}

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=neutron \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/neutron.git \
    --build-arg PROFILES="fluent neutron linuxbridge openvswitch" \
    --build-arg PIP_PACKAGES="pycrypto" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/neutron:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/neutron:${IMAGE_TAG}

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=neutron \
    --build-arg FROM=docker.io/ubuntu:18.04 \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/neutron.git \
    --build-arg PROFILES="fluent neutron linuxbridge openvswitch" \
    --build-arg PIP_PACKAGES="pycrypto" \
    --build-arg DIST_PACKAGES="ethtool lshw" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/neutron:${IMAGE_TAG}-sriov-1804
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/neutron:${IMAGE_TAG}-sriov-1804

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=nova \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/nova.git \
    --build-arg PROFILES="fluent nova ceph linuxbridge openvswitch configdrive qemu apache" \
    --build-arg PIP_PACKAGES="pycrypto" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/nova:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/nova:${IMAGE_TAG}

sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
    /loci \
    --build-arg PROJECT=horizon \
    --build-arg FROM=${FROM} \
    --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
    --build-arg PROJECT_REPO=https://gitee.com/mirrors/horizon.git \
    --build-arg PROFILES="fluent horizon apache" \
    --build-arg PIP_PACKAGES="pycrypto" \
    --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
    --tag docker.io/starlingx4arm/horizon:${IMAGE_TAG}
sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/horizon:${IMAGE_TAG}

# sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
#     /loci \
#     --build-arg PROJECT=senlin \
#     --build-arg FROM=${FROM} \
#     --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
#     --build-arg PROFILES="fluent senlin" \
#     --build-arg PIP_PACKAGES="pycrypto" \
#     --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
#     --tag docker.io/starlingx4arm/senlin:${IMAGE_TAG}
# sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/senlin:${IMAGE_TAG}

# sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
#     /loci \
#     --build-arg PROJECT=congress \
#     --build-arg FROM=${FROM} \
#     --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
#     --build-arg PROFILES="fluent congress" \
#     --build-arg PIP_PACKAGES="pycrypto python-congressclient" \
#     --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
#     --tag docker.io/starlingx4arm/congress:${IMAGE_TAG}
# sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/congress:${IMAGE_TAG}

# sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
#     /loci \
#     --build-arg PROJECT=magnum \
#     --build-arg FROM=${FROM} \
#     --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
#     --build-arg PROFILES="fluent magnum" \
#     --build-arg PIP_PACKAGES="pycrypto" \
#     --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
#     --tag docker.io/starlingx4arm/magnum:${IMAGE_TAG}
# sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/magnum:${IMAGE_TAG}

# sudo docker exec docker-in-docker docker build --force-rm --pull --no-cache \
#     /loci \
#     --build-arg PROJECT=ironic \
#     --build-arg FROM=${FROM} \
#     --build-arg PROJECT_REF=${OPENSTACK_VERSION} \
#     --build-arg PROFILES="fluent ironic ipxe ipmi qemu tftp" \
#     --build-arg PIP_PACKAGES="pycrypto" \
#     --build-arg DIST_PACKAGES="iproute2" \
#     --build-arg WHEELS=starlingx4arm/requirements:${IMAGE_TAG} \
#     --tag docker.io/starlingx4arm/ironic:${IMAGE_TAG}
# sudo docker exec docker-in-docker docker push docker.io/starlingx4arm/ironic:${IMAGE_TAG}
